package com.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.util.Design;
import com.util.Shelf;
import com.util.Workspace;

public class GoRillaApp extends JApplet {
	private JTextField textField;
	private Workspace workspaceObj;
	private final String USER = "TestUser";
	private final String PASS = "password";
	private final String DESIGN = "classtest";
	private Image designImg;
	private BufferedImage bimg;
	private Graphics g;
	private TextField tf;
	private double x, y;
	private ArrayList<Shelf> als;
	private Shelf checkShelf;
	
	
	public void init() {
		JPanel buttonPanel = new JPanel();
		JButton btnGetLoc = new JButton("Get Location");
		tf = new TextField();
		buttonPanel.add(tf);
		tf.setColumns(10);
		buttonPanel.add(btnGetLoc);
		add(buttonPanel, BorderLayout.NORTH);
		
		workspaceObj = new Workspace(USER, PASS);
		workspaceObj.setWorkingDesign(DESIGN);
		Design d = workspaceObj.getWorkingDesign();
		designImg = d.getLibrary().getImg();
		als = workspaceObj.getWorkingDesign().getShelves();

		bimg = (BufferedImage) designImg;
		g = bimg.getGraphics();
		
		btnGetLoc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String callNum = tf.getText().toString();
				
				for(int i = 0; i < als.size(); i ++){
					checkShelf = als.get(i);
					String b = checkShelf.getBegId();
					if (b.equals(callNum)){
						g.setColor(Color.RED);
						g.fillOval((int)checkShelf.getX(), (int)checkShelf.getY(), 5, 5);
						g.drawOval((int)checkShelf.getX()-3, (int)checkShelf.getY()-3, 10, 10);
						g.setColor(Color.BLACK);
						g.drawString(callNum, (int)checkShelf.getX() + 8, (int)checkShelf.getY() + 8);
						repaint();
					}
				}
			}
		});
	}

	public void paintComponent(Graphics g) {
		g.drawOval(20, 20, 50, 50);
	}

	public void update(Graphics g) {
		g.drawImage(designImg, 0, 0, this);
	}

	public void paint(Graphics g) {
		update(g);
	}

}
